package com.ckzp.core.lc;

import java.util.*;

/**
 * Created by 51594 on 2022/8/16.
 */
public class SignTool {
    /**
     * 微信支付签名算法sign
     *
     * @param maps
     * @return
     */
    public static String createSign2(Map<String, Object> maps, String key) {
        StringBuffer sb = new StringBuffer();
        Set es = maps.entrySet();//所有参与传参的参数按照accsii排序（升序）
        Iterator it = es.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            String k = (String) entry.getKey();
            Object v = entry.getValue();
            if (null != v && !"".equals(v)
                    && !"sign".equals(k) && !"key".equals(k)) {
                sb.append(k + "=" + v + "&");
            }
        }
        sb.append("key=" + key);
        //System.out.println(sb.toString());
        String sign = Md5.MD5(sb.toString()).toUpperCase();
        //System.out.println("---------" + sign);
        return sign;
    }


    /**
     * 微信支付签名算法sign
     * 该签名才正确
     *
     * @param maps
     * @param publickey
     * @return
     */
    public static String createSign(Map<String, String> maps, String publickey) {
        StringBuffer sb = new StringBuffer();
        String[] keys = maps.keySet().toArray(new String[0]);
        Arrays.sort(keys);
        for (int k = 0; k < keys.length; k++) {
            String key = keys[k];
            String v = maps.get(key);
            if (null != v && !"".equals(v)
                    && !"sign".equals(k) && !"key".equals(k)) {
                sb.append(key + "=" + v + "&");
            }
        }
        sb.append("key=" + publickey);
        //System.out.println(sb.toString());
        String sign = Md5.MD5(sb.toString().toLowerCase());
        return sign;
    }

    public static void main(String[] arg) {
        Map m = new HashMap();
        m.put("d", "d");
        m.put("u", "a");
        m.put("f", "f");
        m.put("v", "b");
        m.put("k", "k");
        m.put("c", "c");
        System.out.println(createSign(m, "d"));
    }
}
